package com.viajero.movil;

import com.viajero.movil.dbviajero.ViajeroSQLiteHelper;
import com.viajero.movil.dominio.Departamento;
import com.viajero.movil.dominio.Distrito;
import com.viajero.movil.dominio.Hotel;
import com.viajero.movil.negocio.DepartamentoNeg;
import com.viajero.movil.negocio.DistritoNeg;
import com.viajero.movil.negocio.HotelNeg;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources.Theme;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

public class ResultadoHotelActivity extends Activity implements OnItemClickListener, OnClickListener {

	Distrito dist;
	DistritoNeg distNeg;
	Departamento depa;
	DepartamentoNeg depaNeg;
	Hotel[] hotel;
	HotelNeg hotelNeg;
	
	
	ViajeroSQLiteHelper vdb;
	Bundle bund;
	
	TextView txtTitulo;
	Button btnRegresar;
	ListView lvHoteles;
	
	
	Dialog dialogo = null;
	TextView dUbicacion;
	TextView dHotel;
	RatingBar dEstrellas;
	TextView dPrecio;
	Button dCerrar;
	Gallery dgallery;
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.resultado_hotel);
		initComponents();
	}

	public void initComponents(){
		
		distNeg = new DistritoNeg();
		depaNeg = new DepartamentoNeg();
		hotelNeg = new HotelNeg();
		
		vdb = new ViajeroSQLiteHelper(this);
		
		bund = getIntent().getExtras();
		String[] busc = new String[]{bund.getString("id_departamento"),bund.getString("estrellas"),bund.getString("precio")};
		hotel = hotelNeg.cargarHoteles(vdb, busc);
		
		txtTitulo = (TextView)findViewById(R.id.txtTitulo);
		txtTitulo.setText("Lista de hoteles en "+bund.getString("departamento")+":" );
		
		lvHoteles = (ListView)findViewById(R.id.lvHoteles);
		AdaptadorHoteles adaptador = new AdaptadorHoteles(this);        
		lvHoteles.setAdapter(adaptador);
		
		lvHoteles.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adp, View vi, int post,
					long arg3) {
				dialogoVerDetalle(post);
				//dialogoDetalle(post).show();
			}
		});
		
		btnRegresar = (Button)findViewById(R.id.btnRegresar);
		btnRegresar.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
	}
	
	public void dialogoVerDetalle(final int pos){
		AlertDialog.Builder builder = new AlertDialog.Builder(this)
		.setCancelable(false)
		.setTitle("Ver detalles")
		.setPositiveButton("Ver", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.cancel();
				dialogoDetalle(pos).show();
			}
		})
		.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.cancel();
			}
		});
		AlertDialog dialog = builder.create();
		dialog.show();
		
	}
	
	@SuppressLint("ParserError")
	public Dialog dialogoDetalle(final int pos){
	
		
		dialogo = new Dialog(ResultadoHotelActivity.this,R.style.DialgoPantallaCompleta);
		dialogo.setContentView(R.layout.detalle_hotel);
		dialogo.setCancelable(false);
		
		//dialogo.setTitle("Hotel");
		
		
		
		dHotel = (TextView)dialogo.findViewById(R.id.dHotel);
		dHotel.setText(hotel[pos].getNombre());
		
		
		
		dEstrellas = (RatingBar)dialogo.findViewById(R.id.dEstrellas);
		dEstrellas.setRating((float)hotel[pos].getEstrellas());
		//dEstrellas.setNumStars((int)hotel[pos].getEstrellas());
		dEstrellas.setEnabled(false);
		
		String dis  = distNeg.leerDistrito(vdb, hotel[pos].getId_distrito());
		dUbicacion = (TextView)dialogo.findViewById(R.id.txtUbicacion);
		dUbicacion.setText(dis+", "+bund.getString("departamento"));
		
		dPrecio = (TextView)dialogo.findViewById(R.id.dPrecio);
		dPrecio.setText(" S/."+ hotel[pos].getTarifa());
		
		dCerrar = (Button)dialogo.findViewById(R.id.dCerrar);
		dCerrar.setOnClickListener(this);
		
		dgallery = (Gallery) dialogo.findViewById(R.id.gallery1);
		dgallery.setAdapter(new ImageAdapter(this));

		/*dgallery.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Toast.makeText(ResultadoHotelActivity.this, "" + position, Toast.LENGTH_SHORT).show();
            }
        });*/
		
		
		return dialogo;
	}
	
	
	class AdaptadorHoteles extends ArrayAdapter {
    	
    	Activity context;
    	
    	AdaptadorHoteles(Activity context) {
    		super(context, R.layout.item_hotel, hotel);
    		this.context = context;
    	}
    	
    	public View getView(int position, View convertView, ViewGroup parent) 
    	{
			View item = convertView;
			ViewHolder holder;
    		
    		if(item == null)
    		{
    			LayoutInflater inflater = context.getLayoutInflater();
    			item = inflater.inflate(R.layout.item_hotel, null);
    			
    			holder = new ViewHolder();
    			holder.txtHotel = (TextView)item.findViewById(R.id.txtHotel);
    			holder.txtUbicacion = (TextView)item.findViewById(R.id.txtUbicacion);
    			holder.txtPrecio = (TextView)item.findViewById(R.id.txtPrecio);
    			holder.imagenHotel= (ImageView)item.findViewById(R.id.imagenHotel);
    			item.setTag(holder);
    		}
    		else
    		{
    			holder = (ViewHolder)item.getTag();
    		}
			
    		String dist = distNeg.leerDistrito(vdb, hotel[position].getId_distrito());
    		
			holder.txtHotel.setText(hotel[position].getNombre());
			holder.txtUbicacion.setText(dist);
			holder.txtPrecio.setText("S/. "+hotel[position].getTarifa());
			return(item);
		}
    }
    
    static class ViewHolder {
    	TextView txtHotel;
    	TextView txtUbicacion;
    	TextView txtPrecio;
    	ImageView imagenHotel;
    }

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		switch (v.getId()) {
		case R.id.dCerrar:
			dialogo.cancel();
			break;

		default:
			break;
		}
		
	}
	
	
	public class ImageAdapter extends BaseAdapter {
	    int mGalleryItemBackground;
	    private Context mContext;

	    private Integer[] mImageIds = {
	    		R.drawable.imghotel1,
	            R.drawable.dhotel1,
	            R.drawable.dhotel2,
	            R.drawable.dhotel3,
	            R.drawable.dhotel4,
	            R.drawable.dhotel5
	    };

	    public ImageAdapter(Context c) {
	        mContext = c;
	       
	        TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
	        mGalleryItemBackground = a.getResourceId(
	                R.styleable.Gallery1_android_galleryItemBackground, 0);
	        a.recycle();
	    }

	    public int getCount() {
	        return mImageIds.length;
	    }

	    public Object getItem(int position) {
	        return position;
	    }

	    public long getItemId(int position) {
	        return position;
	    }

	    public View getView(int position, View convertView, ViewGroup parent) {
	        ImageView i = new ImageView(mContext);

	        i.setImageResource(mImageIds[position]);
	        i.setLayoutParams(new Gallery.LayoutParams(150, 100));
	        i.setScaleType(ImageView.ScaleType.FIT_XY);
	        i.setBackgroundResource(mGalleryItemBackground);

	        return i;
	    }
	}

}
